/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_fixed.service;

import com.yuecai.modules.yc_fixed.domain.YcFixed;
import com.yuecai.modules.yc_fixed.repository.YcFixedRepository;
import com.yuecai.modules.yc_fixed.service.dto.YcFixedDto;
import com.yuecai.modules.yc_fixed.service.dto.YcFixedQueryCriteria;
import com.yuecai.modules.yc_fixed.service.mapstruct.YcFixedMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author dm
* @date 2020-07-12
**/
@Service
@RequiredArgsConstructor
public class YcFixedService {

    private final YcFixedRepository ycFixedRepository;
    private final YcFixedMapper ycFixedMapper;

    
    public Map<String,Object> queryAll(YcFixedQueryCriteria criteria, Pageable pageable){
        Page<YcFixed> page = ycFixedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycFixedMapper::toDto));
    }

    
    public List<YcFixedDto> queryAll(YcFixedQueryCriteria criteria){
        return ycFixedMapper.toDto(ycFixedRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    
    @Transactional
    public YcFixedDto findById(Long id) {
        YcFixed ycFixed = ycFixedRepository.findById(id).orElseGet(YcFixed::new);
        ValidationUtil.isNull(ycFixed.getId(),"YcFixed","id",id);
        return ycFixedMapper.toDto(ycFixed);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public YcFixedDto create(YcFixed resources) {
        return ycFixedMapper.toDto(ycFixedRepository.save(resources));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(YcFixed resources) {
        YcFixed ycFixed = ycFixedRepository.findById(resources.getId()).orElseGet(YcFixed::new);
        ValidationUtil.isNull( ycFixed.getId(),"YcFixed","id",resources.getId());
        ycFixed.copy(resources);
        ycFixedRepository.save(ycFixed);
    }

    
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            ycFixedRepository.deleteById(id);
        }
    }

    
    public void download(List<YcFixedDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcFixedDto ycFixed : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("位置", ycFixed.getValue());
            map.put("描述", ycFixed.getDetails());
            map.put("内容", ycFixed.getContent());
            map.put(" createTime",  ycFixed.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}